package rpcsupport

import (
	"log"
	"net"
	"net/rpc"
)

func ServeRpc(host string, service interface{}) error {
	_ = rpc.Register(service)
	l, err := net.Listen("tcp", host)
	if err != nil {
		return err
	}

	log.Printf("listening on %s\n", host)

	for {
		conn, err := l.Accept()
		if err != nil {
			log.Printf("accept error: %s\n", err.Error())
			continue
		}

		go rpc.ServeConn(conn)
	}
}

func NewRpcClient(host string) (*rpc.Client, error) {
	return rpc.Dial("tcp", host)
}
